package com.nanjing.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.nanjing.dao.StuDao;
import com.nanjing.util.DBUtil;
import com.nanjing.vo.Student;

public class StuDaoImpl implements StuDao {

	@Override
	public int addStu(Student stu) {
		
		int rs = 0 ;
		String sql = "insert into stu (id,uname,password,sex,studentid,groupid) values (stu_id.nextval,?,?,?,?,?)" ;
		Connection conn = DBUtil.getConnection() ;
		PreparedStatement ps = null ;
		
		try {
			ps = conn.prepareStatement(sql) ;
			ps.setString(1, stu.getUname());
			ps.setString(2, stu.getPassword());
			ps.setBoolean(3, stu.isSex());
			ps.setString(4, stu.getStudentId());
			ps.setString(5, stu.getGroupId());
			rs = ps.executeUpdate() ;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.free(conn, ps, null);
		}
		return rs;
	}

	@Override
	public Student findStu(String studentId, String password) {
		
		Student stu = null ;
		String sql = "select * from stu where studentId=? and password=?" ;
		Connection conn = DBUtil.getConnection() ;
		PreparedStatement ps = null ;
		ResultSet rs = null ;
		
		try {
			ps = conn.prepareStatement(sql) ;
			ps.setString(1, studentId);
			ps.setString(2, password) ;
			rs = ps.executeQuery() ;
			if(rs.next()){
				stu = new Student() ;
				stu.setId(rs.getInt("id"));
				stu.setUname(rs.getString("uname"));
				stu.setPassword(rs.getString("password"));
				stu.setSex(rs.getBoolean("sex"));
				stu.setStudentId(rs.getString("studentId"));
				stu.setGroupId(rs.getString("groupId"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.free(conn, ps, rs);
		}
		
		return stu;
	}

}
